package com.chinasoft.maysham.manager.controller.component;

import com.chinasoft.maysham.manager.controller.BaseController;
import com.chinasoft.maysham.manager.model.Component;
import com.chinasoft.maysham.manager.model.ComponentStatusEnum;
import com.chinasoft.maysham.manager.request.ComponentStartReq;
import com.chinasoft.maysham.manager.response.ComponentStatus;
import com.chinasoft.maysham.manager.response.PageResult;
import com.chinasoft.maysham.manager.service.component.ComponentService;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping(value = "/projects/{projectId}/components")
public class ComponentController extends BaseController {
    @Resource
    private ComponentService componentService;

    @GetMapping("/page")
    public PageResult<Component> findPage(@PathVariable String projectId, @RequestParam(required = false) String keyword,
                                          @RequestParam(required = false, defaultValue = "0") int page,
                                          @RequestParam(required = false, defaultValue = "10") int size) {
        return componentService.findPage(projectId, keyword, PageRequest.of(page, size));
    }

    @PostMapping("/create")
    public Component create(@PathVariable String projectId, @RequestBody Component request) {
        return componentService.create(projectId, request);
    }

    @DeleteMapping("/delete")
    public Component delete(@PathVariable String projectId, @RequestParam String componentId) {
        return componentService.delete(projectId, componentId);

    }

    @PutMapping("/update")
    public Component update(@PathVariable String projectId, @RequestBody Component component) {
        return componentService.update(projectId, component);
    }

    @PostMapping("/start")
    public ComponentStatus start(@RequestBody ComponentStartReq startReq) {
        return componentService.start(startReq.getComponentId());
    }
}
